# -*- coding: utf-8 -*-
import scrapy
from zju_spider.items import *
from zju_spider.DBUtil import db
import sys

reload(sys)
sys.setdefaultencoding('utf-8')


# 公司高管爬虫
class ManagerSpider(scrapy.Spider):
	name = 'ManagerSpider'
	allowed_domains = [u'vip.stock.finance.sina.com.cn']
	custom_settings = {
		'ITEM_PIPELINES': {
			'zju_spider.pipelines.ZjuSpiderPipeline': 300
		}
	}

	def start_requests(self):
		select_sql = 'select * from company'
		select_result = db.select(select_sql)
		# select_result = [select_result[5]]
		for item in select_result:
			if item[0] >= 1:
				code = item[2]
				url = 'http://vip.stock.finance.sina.com.cn/corp/go.php/vCI_CorpManager/stockid/' + code + '.phtml'
				yield scrapy.Request(url, self.parse, meta={'code': code})

	def parse(self, response):
		code = response.meta['code']
		for i in response.css('#comInfo1'):
			for item_href in i.css('a::attr(href)').getall():
				corp_manager_info_url = response.urljoin(item_href).encode('gbk')
				yield scrapy.Request(corp_manager_info_url, callback=self.corp_manager_info_parse, meta={'code': code})

	def corp_manager_info_parse(self, response):
		table1_selector = response.css('#Table1 tbody div')
		manager_info_item = CorpManagerInfoItem()
		manager_info_item['code'] = response.meta['code']
		manager_info_item['name'] = table1_selector[0].css('::text').get()
		manager_info_item['sex'] = table1_selector[1].css('::text').get()
		manager_info_item['birth'] = table1_selector[2].css('::text').get()
		manager_info_item['education'] = table1_selector[3].css('::text').get()
		manager_info_item['detail'] = response.css('.graybgH::text').get()
		table3_selector = response.css('#Table3')
		div3_selector = table3_selector.css('tbody tr')
		positions = []
		for index, i in enumerate(div3_selector):
			item = i.css('div::text')
			position_item = ManagerPositionItem()
			position_item['title'] = item[0].get()
			position_item['start_time'] = item[1].get()
			if len(item) == 3:
				position_item['end_time'] = item[2].get()
			else:
				position_item['end_time'] = None
			positions.append(position_item)
		manager_info_item['position'] = positions
		yield manager_info_item
